package com.screx.mcloud.lib.view.dialog

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.text.InputFilter
import android.view.*
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.ba.base.R
import com.ba.base.utils.ThemeHelper
import com.ba.base.view.web.H5VideoWebView
import qsos.lib.base.callback.OnTListener


/**
 *
 *
 * 弹窗工具
 */
class DialogUtils {

    fun sure(
        context: Context,
        message: String,
        title: String = "提示",
        listener: OnTListener<Boolean>
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title)
        builder.setMessage(message)
        builder.setCancelable(true)
        builder.setPositiveButton(
            "是"
        ) { dialog, _ ->
            listener.back(true)
            dialog.dismiss()
        }
        builder.setNegativeButton(
            "否"
        ) { dialog, _ ->
            listener.back(false)
            dialog.dismiss()
        }
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
        if (context is Activity){
            //https://blog.csdn.net/qq_38356174/article/details/87856639
            //放在show()之后，不然有些属性是没有效果的，比如height和width
            val dialogWindow: Window? = dialog.window
            val m: WindowManager = context.windowManager
            val d = m.defaultDisplay // 获取屏幕宽、高
            val p: WindowManager.LayoutParams = dialogWindow!!.getAttributes() // 获取对话框当前的参数值
            // 设置宽度
            // 设置宽度
            p.width = (d.width * 0.95).toInt() // 宽度设置为屏幕的0.95
            p.gravity = Gravity.CENTER //设置位置
            //p.alpha = 0.8f;//设置透明度
            //p.alpha = 0.8f;//设置透明度
            dialogWindow?.attributes=p
        }
        //修改两个按钮的颜色
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLACK)
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK)
    }

    fun sure(
        context: Context,
        title: String?,
        message: String,
        action: String?
    ) {
        val builder = AlertDialog.Builder(context)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_sure, null)
        val titleView = view.findViewById<TextView>(R.id.dialog_sure_title)
        val contentView = view.findViewById<TextView>(R.id.dialog_sure_content)
        val submitView = view.findViewById<Button>(R.id.dialog_sure_submit)
        submitView.setBackgroundColor(ThemeHelper.mThemeData.value!!.type.color)
        contentView.text = message
        titleView.text = title ?: "提示"
        submitView.text = action ?: "好的"
        builder.setView(view)
        builder.setCancelable(false)
        val dialog = builder.create()
        submitView.setOnClickListener {
            dialog.dismiss()
        }
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
    }

    fun input(
        context: Context,
        title: String?,
        message: String,
        limit: Int = 20,
        listener: OnTListener<String?>
    ) {
        val builder = AlertDialog.Builder(context)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_input, null)
        val editText = view.findViewById<EditText>(R.id.dialog_input_content)
        val closeText = view.findViewById<TextView>(R.id.dialog_input_close)
        val submitText = view.findViewById<TextView>(R.id.dialog_input_submit)
        builder.setTitle(title)
        editText.setText(message)
        editText.filters = arrayOf(
            InputFilter.LengthFilter(limit)
        )
        builder.setView(view)
        builder.setCancelable(false)
        val dialog = builder.create()
        submitText.setOnClickListener {
            dialog.dismiss()
            listener.back(editText.text.toString().trim())
        }
        closeText.setOnClickListener {
            dialog.dismiss()
            listener.back(null)
        }
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
    }

    fun web(
        context: Context,
        url: String
    ) {
        val builder = AlertDialog.Builder(context)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_web, null)
        val webView = view.findViewById<H5VideoWebView>(R.id.dialog_web_content)
        val closeView = view.findViewById<TextView>(R.id.dialog_web_close)
        webView.loadUrl(url)
        builder.setView(view)
        builder.setCancelable(true)
        val dialog = builder.create()
        closeView.setOnClickListener {
            dialog.dismiss()
        }
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
    }
}